﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace BCS.Business
{
        public static class SpanQueryPDF
        {
                public static String CreatePdfDocument(DateTime startTime, DateTime endTime)
                {
                        String PdfDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Pdf");
                        String PdfExtension = ".pdf";

                        if (!Directory.Exists(PdfDirectory))
                        {
                                Directory.CreateDirectory(PdfDirectory);
                        }

                        String title = "连铸  号机" + startTime.ToString("yyyy年MM月dd日 HH时mm分ss") + "--" +  endTime.ToString("yyyy年MM月dd日 HH时mm分ss") + "送坯统计表";

                        String fileName = Path.Combine(PdfDirectory, title + PdfExtension);

                        Document document = PDFUtility.CreatePdfDocument(fileName);

                        document.Open();

                        PDFUtility.CreateHeader(document, title);

                        PdfPTable mainTable = new PdfPTable(new float[] { 0.25f, 0.75f });

                        //表头
                        mainTable.AddCell(PDFUtility.CreateSmallPdfCell("起始时间"));
                        mainTable.AddCell(PDFUtility.CreateSmallPdfCell(startTime.ToString("yyyy年MM月dd日 HH时mm分ss")));

                        mainTable.AddCell(PDFUtility.CreateSmallPdfCell("结束时间"));
                        mainTable.AddCell(PDFUtility.CreateSmallPdfCell(endTime.ToString("yyyy年MM月dd日 HH时mm分ss")));


                        var records = BusinessCache.Instance.Records.Where(i => i.CreateTime > startTime && i.CreateTime < endTime);


                        mainTable.AddCell(PDFUtility.CreateSmallPdfCell("数量（根）"));
                        mainTable.AddCell(PDFUtility.CreateSmallPdfCell(records.Sum(i => i.Count).ToString()));

                        document.Add(mainTable);

                        PdfPTable FixTable = new PdfPTable(new float[] { 0.20f, 0.60f, 0.20f });

                        PdfPCell cell = PDFUtility.CreateSmallPdfCell("人工修正");
                        cell.HorizontalAlignment = 1;
                        cell.Colspan = 3;
                        FixTable.AddCell(cell);

                        cell = PDFUtility.CreateSmallPdfCell("数量");
                        cell.HorizontalAlignment = 1;
                        FixTable.AddCell(cell);

                        cell = PDFUtility.CreateSmallPdfCell("说明");
                        cell.HorizontalAlignment = 1;
                        FixTable.AddCell(cell);

                        cell = PDFUtility.CreateSmallPdfCell("签字");
                        cell.HorizontalAlignment = 1;
                        FixTable.AddCell(cell);

                        for (int i = 0; i < 18; i++)
                        {
                                FixTable.AddCell(PDFUtility.BlankCell);
                        }

                        document.Add(FixTable);

                        PDFUtility.CreateFooter(document);

                        document.Close();

                        return fileName;
                }
        }
}
